package com.itlong.cloud.rabbitmq.consumer;

import com.itlong.cloud.POJO.DTO.property.ElevatorFloorChangeDTO;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.property.service.IPropertyEquipService;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      电梯起始楼层变更业务处理消费者
 * </desc>
 *
 * @createDate 2018/07/10
 */
@Component
public class ElevatorFloorChangeConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(ElevatorFloorChangeConsumer.class);

    @Autowired
    IPropertyEquipService iPropertyEquipService;

    /**
     * <desc>
     *      根据电梯设备的楼层范围变更重新设置物业临时授权的用户楼层权限
     * <desc/>
     *
     * @param   message 消息数据
     * @author Jiaqi.X
     * @createDate 2017/11/13
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_CHANGE_USER_ELEVATOR_FLOOR_AUTH_QUEUE)
    public void process(Message message, Channel channel) throws IOException {
        byte[] body = message.getBody();
        try {
            TimeTaskDeadMessageQueuePO po= JsonUtil.toObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            Map<String,Object> map=JsonUtil.toObject(po.getMessageContent(),HashMap.class);
            ElevatorFloorChangeDTO dto = JsonUtil.toObject(JsonUtil.toJSON(map.get("elevator")), ElevatorFloorChangeDTO.class);
            iPropertyEquipService.changeUserElevatorFloorAuthByDeviceFloorChange(dto);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            throw new DataAccessException("【RabbitMQ_根据电梯设备的楼层范围变更重新设置物业临时授权的用户楼层权限】:处理异常"+new String(body), e);
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            LOG.info("RabbitMQ_根据电梯设备的楼层范围变更重新设置物业临时授权的用户楼层权限:" + new String(body));
        }
    }
}